package com.jeesite.modules.ic.api;

import com.jeesite.common.entity.Page;
import com.jeesite.common.lang.StringUtils;
import com.jeesite.modules.config.Result;
import com.jeesite.modules.ic.channel.CardDataService;
import com.jeesite.modules.ic.entity.IcCard;
import com.jeesite.modules.ic.service.IcCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/app/api")
public class ApiCardController {

    @Autowired
    private IcCardService icCardService;




    /**
     * 解绑设备
     * @param icCard
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/jiebang")
    public Result jiebang(IcCard icCard, HttpServletRequest request, HttpServletResponse response){
        icCard = icCardService.get(icCard);
        icCard.setUserId(null);
        icCardService.save(icCard);
        return Result.buildSuccessResult();
    }

    /**
     * 编辑流量卡
     * @param icCard
     * @param request
     * @param response
     * @return
     */
    @RequestMapping("/saveCard")
    public Result saveCard(IcCard icCard, HttpServletRequest request, HttpServletResponse response){
        IcCard icCard1 = icCardService.get(icCard);
        icCard1.setName(icCard.getName());
        icCardService.save(icCard1);
        return Result.buildSuccessResult();
    }

    @RequestMapping("/getCardListByUserId")
    public Result getCardListByUserId(IcCard icCard, HttpServletRequest request, HttpServletResponse response){
        String user_id = request.getAttribute("user_id").toString();
        icCard.setUserId(user_id);
        icCard.setPage(new Page(request,response));
        Page<IcCard> page = icCardService.findPage(icCard);
        return Result.buildSuccessResult(page);
    }

    @RequestMapping("/addCard")
    public Result addCard(IcCard icCard, HttpServletRequest request, HttpServletResponse response){
        String user_id = request.getAttribute("user_id").toString();
        String iccid = icCard.getIccid();
        IcCard oneCard = icCardService.getOneCard(iccid);
        if(oneCard==null){
            return Result.buildFailureResult("当前流量卡不存在");
        }else{
            String userId = oneCard.getUserId();
            if(StringUtils.isNotEmpty(userId)){
                return Result.buildFailureResult("当前流量卡已使用");
            }
        }
        oneCard.setUserId(user_id);
        icCardService.save(oneCard);
        return Result.buildSuccessResult();
    }

    @RequestMapping("/getCardInfo")
    public Result getCardInfo(IcCard icCard, HttpServletRequest request, HttpServletResponse response){
        String user_id = request.getAttribute("user_id").toString();
        String id = icCard.getId();
        icCard.setUserId(user_id);
        icCard = icCardService.get(id);
        return Result.buildSuccessResult(icCard);
    }

    @RequestMapping("/delCard")
    public Result delCard(IcCard icCard, HttpServletRequest request, HttpServletResponse response){
        String user_id = request.getAttribute("user_id").toString();
        String id = icCard.getId();
        IcCard icCard1 = icCardService.get(id);
        icCard1.setUserId(null);
        icCardService.save(icCard1);
        return Result.buildSuccessResult();
    }


}
